package com.eqdd.yiqidian.ui.share;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.GroupMemberListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.subscribers.DisposableSubscriber;
import io.rong.imlib.model.Conversation;

public class ShareUserActivity extends BaseActivity {
    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;
    private ArrayList<SelectBean> selectBeans = new ArrayList<>();
    private ArrayList<Object> datas;
    private String groupId;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "请选择");
        initTopRightText("发送", (i) -> {
            if (selectBeans.size() > 0) {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(Config.SELECTED_BEANS, selectBeans);
                setResult(Config.SUCCESS, intent);
                finish();
            } else {
                ToastUtil.showShort("请进行人员选择");
            }
        });
    }

    @Override
    public void initData() {
        groupId = getIntent().getStringExtra("groupId");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_04, new SlimInjector<SelectBean>() {
                    @Override
                    public void onInject(SelectBean data, IViewInjector injector) {
                        injector.text(R.id.tv_content, data.getContent())
                                .imageCircle(R.id.iv_head, data.getHeadImg())
                                .checked(R.id.checkbox, data.isChecked());
                    }
                }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    SelectBean bean = (SelectBean) slimAdapterEx.getDataItem(position);
                    if (!bean.isChecked() && selectBeans.size() >= 5) {
                        ToastUtil.showShort("最多5人");
                        return;
                    }
                    bean.setChecked(!bean.isChecked());
                    if (bean.isChecked()) {
                        selectBeans.add(bean);
                    } else {
                        selectBeans.remove(bean);
                    }
                    slimAdapterEx.notifyItemChanged(position);
                });
    }

    private void refresh() {
        datas = new ArrayList<>();
        OkGo.<HttpResult<List<GroupMemberListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GROUP_USER_LIST)
                .params("groupid", groupId)
                .execute(new DialogCallBack<HttpResult<List<GroupMemberListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<GroupMemberListResultBean>>> response) {
                        Flowable.fromIterable(response.body().getItems())
                                .subscribe(dispo);
                    }
                });
    }


    DisposableSubscriber dispo = new DisposableSubscriber<GroupMemberListResultBean>() {
        @Override
        public void onNext(GroupMemberListResultBean friendListResultBean) {

            SelectBean bean = new SelectBean();
            bean.setGuid(friendListResultBean.getGuid());
            bean.setHeadImg(friendListResultBean.getIphoto());
            bean.setContent(friendListResultBean.getUpname());
            bean.setType(Conversation.ConversationType.PRIVATE);
            datas.add(bean);
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {

            slimAdapterEx.updateData(datas);
        }
    };

    @Override
    public void setView() {
        refresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        if (dispo != null && !dispo.isDisposed()) {
            dispo.dispose();
        }
        super.onDestroy();
    }
}
